Ext.define('cloud.controller.Standards', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    config: {
        refs: {
            standards: 'standards',
            standardsadd: 'standardsadd',
            standardsedit: 'standardsedit',
            standards_add: 'standards #standards_add',
            standards_submit: 'standardsadd #standards_submit',
            standards_edit_submit: 'standardsedit #standards_edit_submit',
            standards_del: 'standardsedit #standards_del',
            select: 'standards #standards_select'
        },
        control: {
            /**
             * show standardsadd page
             */
            standards_add: {
                tap: function () {
                    var standards = this.getStandards();
                    var standsadd=this.getStandardsadd()||Ext.create('cloud.view.standards.StandardsAdd');
                    standards.push(standsadd);
                }
            },
            /**
             * show standardsedit page
             */
            select: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var standardsView = this.getStandards();
                    var standardsstore = Ext.getStore('StandardsAllFindStore').load({params:{caid:localStorage.getItem('caid')}});
                    var standardsedit = this.getStandardsedit()||Ext.create('cloud.view.standards.StandardsEdit');
                    standardsedit.setRecord(record);
                    standardsView.push(standardsedit);
                    Ext.Viewport.unmask();
                }
            },
            /**
             * add standards
             */
            standards_submit: {
                tap: function () {
                    var name = Ext.getCmp('standsadd_name').getValue();
                    var explain = Ext.getCmp('explains').getValue();
                    if (Ext.isEmpty(name)) {
                        Ext.Msg.alert('请输入分类名称');
                        return
                    }
                    if (Ext.isEmpty(explain)) {
                        Ext.Msg.alert('请输入规则介绍');
                        return
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getStandardsadd().submit({
                        url: GLOBAL.api_url + 'standards_add&caid=' + localStorage.getItem('caid'),
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * edit standards
             */
            standards_edit_submit: {
                tap: function () {
                    var name = Ext.getCmp('standsedit_name').getValue();
                    var explain = Ext.getCmp('standsedit_explains').getValue();
                    if (Ext.isEmpty(name)) {
                        Ext.Msg.alert('请输入分类名称');
                        return
                    }
                    if (Ext.isEmpty(explain)) {
                        Ext.Msg.alert('请输入规则介绍');
                        return
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getStandardsedit().submit({
                        url: GLOBAL.api_url + 'standards_update',
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * delete standards
             */
            standards_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getStandardsedit().submit({
                                url: GLOBAL.api_url + 'standards_delete',
                                scope: this,
                                method: 'POST',
                                success: this.executeSuccess
                            });
                        }
                    }, this);
                }
            }
        }
    },
    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            Ext.getStore('StandardsAllFindStore').load({params:{caid:localStorage.getItem('caid')}});
            this.getStandards().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    }
});